package com.slkj.shilixiaoyuanapp.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.slkj.shilixiaoyuanapp.R;

/**
 * Created  on 2018-07-16
 * @author 唐开阔
 * @describe 显示剩余字数的EditTetxt
 */
public class NumberEditText extends FrameLayout  {
    private EditText editText;
    private TextView textView;
    private String hint;
    private int max;
    private boolean focusable;
    private String text;

    public NumberEditText(@NonNull Context context) {
        this(context,null);
    }

    public NumberEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public NumberEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NumberEditText);
        max = typedArray.getInt(R.styleable.NumberEditText_max, 100);
        hint = typedArray.getString(R.styleable.NumberEditText_hint);
        text = typedArray.getString(R.styleable.NumberEditText_text);
        focusable = typedArray.getBoolean(R.styleable.NumberEditText_focusable,true);
        typedArray.recycle();
        LayoutInflater.from(context).inflate(R.layout.layout_number_edittext, this, true);
        editText = findViewById(R.id.edit);
        textView = findViewById(R.id.tv_nums);
        editText.setHint(hint);
        editText.setMaxEms(max);
        editText.setText(text);
        editText.setFocusable(focusable);
        setNumsText();
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setNumsText();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        editText.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //触摸的是EditText并且当前EditText可以滚动则将事件交给EditText处理；否则将事件交由其父类处理
                if ((v.getId() == R.id.edit && canVerticalScroll(editText))) {
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                    }
                }
                return false;
            }
        });

    }

    /**
     * EditText竖直方向是否可以滚动
     * @param editText 需要判断的EditText
     * @return true：可以滚动  false：不可以滚动
     */
    private boolean canVerticalScroll(EditText editText) {
        //滚动的距离
        int scrollY = editText.getScrollY();
        //控件内容的总高度
        int scrollRange = editText.getLayout().getHeight();
        //控件实际显示的高度
        int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() -editText.getCompoundPaddingBottom();
        //控件内容总高度与实际显示高度的差值
        int scrollDifference = scrollRange - scrollExtent;

        if(scrollDifference == 0) {
            return false;
        }

        return (scrollY > 0) || (scrollY < scrollDifference - 1);
    }

    private void  setNumsText(){
        int size =  TextUtils.isEmpty(editText.getText().toString())?0 :editText.getText().toString().length();
        textView.setText(size+"/"+max);
    }

    public String getText() {
        return editText.getText().toString();
    }
    public void setText(String s) {
        editText.setText(s);
    }

    public void setFocusable(boolean focusable){
        editText.setFocusable(focusable);
    }
}
